﻿using Common.XmlHandle;
using Network.UpdateData;
using SFJDManagement.SocketHandle;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SFJDManagement
{
    /// <summary>
    /// Page_ConfigSet.xaml 的交互逻辑
    /// </summary>
    public partial class Page_ConfigSet : Page
    {
        public StackPanel layout = new StackPanel();
        private TextBox tb1;

        public Page_ConfigSet()
        {
            InitializeComponent();
            Ini();
            Content = layout;
        }

        private void Ini()
        {
            layout.HorizontalAlignment = HorizontalAlignment.Center;

            Binding bind = new Binding();
            bind.Source = Runtime.config;
            bind.Mode = BindingMode.TwoWay;
            Label l1 = new Label();
            l1.FontSize = 16;
            l1.Content = "用户姓名：";
            layout.Children.Add(l1);
            tb1 = new TextBox();
            tb1.FontSize = 18;
            tb1.Height = 30;
            tb1.Width = 200;
            bind.Path = new PropertyPath("UserName");
            tb1.SetBinding(TextBox.TextProperty, bind);
            //tb.DataContext = config.UserName;
            layout.Children.Add(tb1);
            CheckBox cb2 = new CheckBox();
            cb2.Content = "  是否为服务器";
            cb2.IsChecked = Runtime.config.IsServer;
            cb2.FontSize = 18;
            layout.Children.Add(cb2);
            CheckBox cb3 = new CheckBox();
            cb3.Content = "  是否管理员";
            cb3.IsChecked = Runtime.config.IsAdmin;
            cb3.FontSize = 18;
            layout.Children.Add(cb3);
            CheckBox cb4 = new CheckBox();
            cb4.Content = "  是否可以自定义";
            cb4.IsChecked = Runtime.config.IsCustomize;
            cb4.FontSize = 18;
            layout.Children.Add(cb4);
            CheckBox cb5 = new CheckBox();
            cb5.Content = "  是否仅查询模式";
            cb5.IsChecked = Runtime.config.IsQueryOnly;
            cb5.FontSize = 18;
            layout.Children.Add(cb5);
            Label l2 = new Label();
            l2.Content = "接收文件的目录：";
            l2.FontSize = 16;
            layout.Children.Add(l2);
            Label l3 = new Label();
            l3.Content = "案件材料存放的目录：";
            l3.FontSize = 16;
            layout.Children.Add(l3);
            TextBox tb3 = new TextBox();
            tb3.FontSize = 18;
            tb3.Text = Runtime.config.OutPath2String();
            layout.Children.Add(tb3);

            Button saveConfig = new Button();
            saveConfig.Content = "保存";
            saveConfig.FontSize = 16;
            saveConfig.Height = 30;
            saveConfig.Margin = new Thickness(0, 30, 0, 0);
            saveConfig.Click += SaveConfig_Click;
            layout.Children.Add(saveConfig);
        }

        private void SaveConfig_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Runtime.config = Runtime.xmlUsers.GetUser(Runtime.config.UserName);
                Runtime.xmlConfig.SetConfig(Runtime.config);
            }
            catch
            {
                new Thread(() =>
                {
                    Packet packet = new Packet(typeof(SC04RegisterUser).GetClient2ServerKey(), Runtime.config.UserGuid, Runtime.config.Coding2String());
                    CSendMsg.SendMsgNoWait(packet.ToBytes());
                    int n = 0;
                    bool isDefind = false;
                    while (!(isDefind = Runtime.xmlUsers.IsDefindUser(Runtime.config.UserName)) & n < 10)
                    {
                        Thread.Sleep(300);
                        n++;
                    }
                    if (isDefind)
                    {
                        Runtime.config = Runtime.xmlUsers.GetUser(Runtime.config.UserName);
                        //new XmlConfig(Runtime.fileConfig).SetConfig(Runtime.config);
                    }
                }).Start();
            }
        }
    }
}
